/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.service;

import com.github.pagehelper.Page;
import com.lvyh.lightframe.job.admin.bean.JobAlarmConfigBean;
import com.lvyh.lightframe.job.admin.bean.JobAlarmLogBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskDefBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskHistoryBean;
import com.lvyh.lightframe.job.admin.bean.JobTaskHistoryDetailBean;
import com.lvyh.lightframe.job.admin.bean.JobUserBean;
import com.lvyh.lightframe.job.admin.bean.JobUserPermitBean;
import com.lvyh.lightframe.job.admin.dao.entity.JobAlarmConfig;
import com.lvyh.lightframe.job.admin.dao.entity.JobAlarmConfigExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobAlarmLog;
import com.lvyh.lightframe.job.admin.dao.entity.JobAlarmLogExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskDef;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskDefExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskHistory;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskHistoryDetail;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskHistoryDetailExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobTaskHistoryExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobUser;
import com.lvyh.lightframe.job.admin.dao.entity.JobUserExample;
import com.lvyh.lightframe.job.admin.dao.entity.JobUserPermit;
import com.lvyh.lightframe.job.admin.dao.entity.JobUserPermitExample;
import com.lvyh.lightframe.job.admin.dao.mapper.JobAlarmConfigMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobAlarmLogMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobTaskDefMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobTaskHistoryDetailMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobTaskHistoryMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobUserMapper;
import com.lvyh.lightframe.job.admin.dao.mapper.JobUserPermitMapper;
import com.lvyh.lightframe.job.admin.pagination.DefaultPageResult;
import com.lvyh.lightframe.job.admin.pagination.PageParam;
import com.lvyh.lightframe.job.admin.pagination.PageUtil;
import com.lvyh.lightframe.job.admin.util.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lvyh 2021/07/14.
 */
@Service
public class JobMainDataService {
    private final static Logger logger = LoggerFactory.getLogger(JobMainDataService.class);

    @Autowired
    private JobUserMapper jobUserMapper;
    @Autowired
    private JobUserPermitMapper jobUserPermitMapper;
    @Autowired
    private JobTaskDefMapper jobTaskDefMapper;
    @Autowired
    private JobTaskHistoryMapper jobTaskHistoryMapper;
    @Autowired
    private JobAlarmConfigMapper jobAlarmConfigMapper;
    @Autowired
    private JobAlarmLogMapper jobAlarmLogMapper;
    @Autowired
    private JobTaskHistoryDetailMapper jobTaskHistoryDetailMapper;


    public boolean deleteJobTaskHistoryDetail(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobTaskHistoryDetailMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobTaskHistoryDetailBean> selectJobTaskHistoryDetail(JobTaskHistoryDetailBean condition) throws Exception {
        List<JobTaskHistoryDetailBean> list = new ArrayList<JobTaskHistoryDetailBean>();
        try {
            JobTaskHistoryDetailExample example = new JobTaskHistoryDetailExample();
            JobTaskHistoryDetailExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }

            List<JobTaskHistoryDetail> queryDataItems = jobTaskHistoryDetailMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobTaskHistoryDetail jobTaskHistoryDetail : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskHistoryDetailBean.class, BeanUtil.toMap(jobTaskHistoryDetail)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobTaskHistoryDetailBean> selectJobTaskHistoryDetailByCondition(JobTaskHistoryDetailBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobTaskHistoryDetail> page = PageUtil.startPage(pageParam);

        try {
            JobTaskHistoryDetailExample example = new JobTaskHistoryDetailExample();
            if (condition != null) {
                JobTaskHistoryDetailExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobTaskHistoryDetail> queryDataItems = jobTaskHistoryDetailMapper.selectByExample(example);
            DefaultPageResult<JobTaskHistoryDetailBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobTaskHistoryDetailBean> list = new ArrayList<JobTaskHistoryDetailBean>();
            for (JobTaskHistoryDetail jobTaskHistoryDetail : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskHistoryDetailBean.class, BeanUtil.toMap(jobTaskHistoryDetail)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobTaskHistoryDetailByCondition(JobTaskHistoryDetailBean condition) throws Exception {
        try {
            JobTaskHistoryDetailExample example = new JobTaskHistoryDetailExample();
            if (condition != null) {
                JobTaskHistoryDetailExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobTaskHistoryDetailMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobTaskHistoryDetail(JobTaskHistoryDetailBean jobTaskHistoryDetailBean) throws Exception {
        if (jobTaskHistoryDetailBean == null || jobTaskHistoryDetailBean.getId() <= 0) {
            return false;
        }
        try {
            jobTaskHistoryDetailBean.setUpdateTime(new Date());
            JobTaskHistoryDetail updateBean = BeanUtil.toBean(JobTaskHistoryDetail.class, BeanUtil.toMap(jobTaskHistoryDetailBean));
            int updateNum = jobTaskHistoryDetailMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobTaskHistoryDetailBean selectJobTaskHistoryDetailById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobTaskHistoryDetail jobTaskHistoryDetail = jobTaskHistoryDetailMapper.selectByPrimaryKey(id);
            if (jobTaskHistoryDetail == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobTaskHistoryDetailBean.class, BeanUtil.toMap(jobTaskHistoryDetail));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobTaskHistoryDetail(JobTaskHistoryDetailBean jobTaskHistoryDetailBean) throws Exception {
        if (jobTaskHistoryDetailBean == null) {
            return -1;
        }
        try {
            jobTaskHistoryDetailBean.setCreateTime(new Date());
            jobTaskHistoryDetailBean.setUpdateTime(new Date());
            JobTaskHistoryDetail insertBean = BeanUtil.toBean(JobTaskHistoryDetail.class, BeanUtil.toMap(jobTaskHistoryDetailBean));
            jobTaskHistoryDetailMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }


    public boolean deleteJobAlarmLog(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobAlarmLogMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobAlarmLogBean> selectJobAlarmLog(JobAlarmLogBean condition) throws Exception {
        List<JobAlarmLogBean> list = new ArrayList<JobAlarmLogBean>();
        try {
            JobAlarmLogExample example = new JobAlarmLogExample();
            JobAlarmLogExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }


            List<JobAlarmLog> queryDataItems = jobAlarmLogMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobAlarmLog jobAlarmLog : queryDataItems) {
                list.add(BeanUtil.toBean(JobAlarmLogBean.class, BeanUtil.toMap(jobAlarmLog)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobAlarmLogBean> selectJobAlarmLogByCondition(JobAlarmLogBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobAlarmLog> page = PageUtil.startPage(pageParam);

        try {
            JobAlarmLogExample example = new JobAlarmLogExample();
            if (condition != null) {
                JobAlarmLogExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobAlarmLog> queryDataItems = jobAlarmLogMapper.selectByExample(example);
            DefaultPageResult<JobAlarmLogBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobAlarmLogBean> list = new ArrayList<JobAlarmLogBean>();
            for (JobAlarmLog jobAlarmLog : queryDataItems) {
                list.add(BeanUtil.toBean(JobAlarmLogBean.class, BeanUtil.toMap(jobAlarmLog)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobAlarmLogByCondition(JobAlarmLogBean condition) throws Exception {
        try {
            JobAlarmLogExample example = new JobAlarmLogExample();
            if (condition != null) {
                JobAlarmLogExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobAlarmLogMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobAlarmLog(JobAlarmLogBean jobAlarmLogBean) throws Exception {
        if (jobAlarmLogBean == null || jobAlarmLogBean.getId() <= 0) {
            return false;
        }
        try {
            jobAlarmLogBean.setUpdateTime(new Date());
            JobAlarmLog updateBean = BeanUtil.toBean(JobAlarmLog.class, BeanUtil.toMap(jobAlarmLogBean));
            int updateNum = jobAlarmLogMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobAlarmLogBean selectJobAlarmLogById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobAlarmLog jobAlarmLog = jobAlarmLogMapper.selectByPrimaryKey(id);
            if (jobAlarmLog == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobAlarmLogBean.class, BeanUtil.toMap(jobAlarmLog));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobAlarmLog(JobAlarmLogBean jobAlarmLogBean) throws Exception {
        if (jobAlarmLogBean == null) {
            return -1;
        }
        try {
            jobAlarmLogBean.setCreateTime(new Date());
            jobAlarmLogBean.setUpdateTime(new Date());
            JobAlarmLog insertBean = BeanUtil.toBean(JobAlarmLog.class, BeanUtil.toMap(jobAlarmLogBean));
            jobAlarmLogMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }


    public boolean deleteJobAlarmConfig(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobAlarmConfigMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobAlarmConfigBean> selectJobAlarmConfig(JobAlarmConfigBean condition) throws Exception {
        List<JobAlarmConfigBean> list = new ArrayList<JobAlarmConfigBean>();
        try {
            JobAlarmConfigExample example = new JobAlarmConfigExample();
            JobAlarmConfigExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getAlarmType() != null) {
                criteria.andAlarmTypeEqualTo(condition.getAlarmType());
            }
            if (condition.getStatus() != null) {
                criteria.andStatusEqualTo(condition.getStatus());
            }
            if (condition.getObjectId() != null) {
                criteria.andObjectIdEqualTo(condition.getObjectId());
            }

            List<JobAlarmConfig> queryDataItems = jobAlarmConfigMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobAlarmConfig jobAlarmConfig : queryDataItems) {
                list.add(BeanUtil.toBean(JobAlarmConfigBean.class, BeanUtil.toMap(jobAlarmConfig)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobAlarmConfigBean> selectJobAlarmConfigByCondition(JobAlarmConfigBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobAlarmConfig> page = PageUtil.startPage(pageParam);

        try {
            JobAlarmConfigExample example = new JobAlarmConfigExample();
            if (condition != null) {
                JobAlarmConfigExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobAlarmConfig> queryDataItems = jobAlarmConfigMapper.selectByExample(example);
            DefaultPageResult<JobAlarmConfigBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobAlarmConfigBean> list = new ArrayList<JobAlarmConfigBean>();
            for (JobAlarmConfig jobAlarmConfig : queryDataItems) {
                list.add(BeanUtil.toBean(JobAlarmConfigBean.class, BeanUtil.toMap(jobAlarmConfig)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobAlarmConfigByCondition(JobAlarmConfigBean condition) throws Exception {
        try {
            JobAlarmConfigExample example = new JobAlarmConfigExample();
            if (condition != null) {
                JobAlarmConfigExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobAlarmConfigMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobAlarmConfig(JobAlarmConfigBean jobAlarmConfigBean) throws Exception {
        if (jobAlarmConfigBean == null || jobAlarmConfigBean.getId() <= 0) {
            return false;
        }
        try {
            jobAlarmConfigBean.setUpdateTime(new Date());
            JobAlarmConfig updateBean = BeanUtil.toBean(JobAlarmConfig.class, BeanUtil.toMap(jobAlarmConfigBean));
            int updateNum = jobAlarmConfigMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobAlarmConfigBean selectJobAlarmConfigById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobAlarmConfig jobAlarmConfig = jobAlarmConfigMapper.selectByPrimaryKey(id);
            if (jobAlarmConfig == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobAlarmConfigBean.class, BeanUtil.toMap(jobAlarmConfig));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobAlarmConfig(JobAlarmConfigBean jobAlarmConfigBean) throws Exception {
        if (jobAlarmConfigBean == null) {
            return -1;
        }
        try {
            jobAlarmConfigBean.setCreateTime(new Date());
            jobAlarmConfigBean.setUpdateTime(new Date());
            JobAlarmConfig insertBean = BeanUtil.toBean(JobAlarmConfig.class, BeanUtil.toMap(jobAlarmConfigBean));
            jobAlarmConfigMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteJobTaskHistory(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobTaskHistoryMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobTaskHistoryBean> selectJobTaskHistory(JobTaskHistoryBean condition) throws Exception {
        List<JobTaskHistoryBean> list = new ArrayList<JobTaskHistoryBean>();
        try {
            JobTaskHistoryExample example = new JobTaskHistoryExample();
            JobTaskHistoryExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }


            List<JobTaskHistory> queryDataItems = jobTaskHistoryMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobTaskHistory jobTaskHistory : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskHistoryBean.class, BeanUtil.toMap(jobTaskHistory)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobTaskHistoryBean> selectJobTaskHistoryByCondition(JobTaskHistoryBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobTaskHistory> page = PageUtil.startPage(pageParam);

        try {
            JobTaskHistoryExample example = new JobTaskHistoryExample();
            if (condition != null) {
                JobTaskHistoryExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobTaskHistory> queryDataItems = jobTaskHistoryMapper.selectByExample(example);
            DefaultPageResult<JobTaskHistoryBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobTaskHistoryBean> list = new ArrayList<JobTaskHistoryBean>();
            for (JobTaskHistory jobTaskHistory : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskHistoryBean.class, BeanUtil.toMap(jobTaskHistory)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobTaskHistoryByCondition(JobTaskHistoryBean condition) throws Exception {
        try {
            JobTaskHistoryExample example = new JobTaskHistoryExample();
            if (condition != null) {
                JobTaskHistoryExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobTaskHistoryMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobTaskHistory(JobTaskHistoryBean jobTaskHistoryBean) throws Exception {
        if (jobTaskHistoryBean == null || jobTaskHistoryBean.getId() <= 0) {
            return false;
        }
        try {
            jobTaskHistoryBean.setUpdateTime(new Date());
            JobTaskHistory updateBean = BeanUtil.toBean(JobTaskHistory.class, BeanUtil.toMap(jobTaskHistoryBean));
            int updateNum = jobTaskHistoryMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobTaskHistoryBean selectJobTaskHistoryById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobTaskHistory jobTaskHistory = jobTaskHistoryMapper.selectByPrimaryKey(id);
            if (jobTaskHistory == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobTaskHistoryBean.class, BeanUtil.toMap(jobTaskHistory));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobTaskHistory(JobTaskHistoryBean jobTaskHistoryBean) throws Exception {
        if (jobTaskHistoryBean == null) {
            return -1;
        }
        try {
            jobTaskHistoryBean.setCreateTime(new Date());
            jobTaskHistoryBean.setUpdateTime(new Date());
            JobTaskHistory insertBean = BeanUtil.toBean(JobTaskHistory.class, BeanUtil.toMap(jobTaskHistoryBean));
            jobTaskHistoryMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteJobTaskDef(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobTaskDefMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobTaskDefBean> selectJobTaskDef(JobTaskDefBean condition) throws Exception {
        List<JobTaskDefBean> list = new ArrayList<JobTaskDefBean>();
        try {
            JobTaskDefExample example = new JobTaskDefExample();
            JobTaskDefExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getStatus() != null) {
                criteria.andStatusEqualTo(condition.getStatus());
            }

            if (condition.getTaskType() != null) {
                criteria.andTaskTypeEqualTo(condition.getTaskType());
            }

            if (!StringUtils.isEmpty(condition.getTaskName())) {
                criteria.andTaskNameEqualTo(condition.getTaskName());
            }

            List<JobTaskDef> queryDataItems = jobTaskDefMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobTaskDef jobTaskDef : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskDefBean.class, BeanUtil.toMap(jobTaskDef)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobTaskDefBean> selectJobTaskDefByCondition(JobTaskDefBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobTaskDef> page = PageUtil.startPage(pageParam);

        try {
            JobTaskDefExample example = new JobTaskDefExample();
            if (condition != null) {
                JobTaskDefExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobTaskDef> queryDataItems = jobTaskDefMapper.selectByExample(example);
            DefaultPageResult<JobTaskDefBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobTaskDefBean> list = new ArrayList<JobTaskDefBean>();
            for (JobTaskDef jobTaskConfig : queryDataItems) {
                list.add(BeanUtil.toBean(JobTaskDefBean.class, BeanUtil.toMap(jobTaskConfig)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobTaskDefByCondition(JobTaskDefBean condition) throws Exception {
        try {
            JobTaskDefExample example = new JobTaskDefExample();
            if (condition != null) {
                JobTaskDefExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobTaskDefMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobTaskDef(JobTaskDefBean jobTaskDefBean) throws Exception {
        if (jobTaskDefBean == null || jobTaskDefBean.getId() <= 0) {
            return false;
        }
        try {
            jobTaskDefBean.setUpdateTime(new Date());
            JobTaskDef updateBean = BeanUtil.toBean(JobTaskDef.class, BeanUtil.toMap(jobTaskDefBean));
            int updateNum = jobTaskDefMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobTaskDefBean selectJobTaskDefById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobTaskDef jobTaskDef = jobTaskDefMapper.selectByPrimaryKey(id);
            if (jobTaskDef == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobTaskDefBean.class, BeanUtil.toMap(jobTaskDef));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobTaskDef(JobTaskDefBean jobTaskDefBean) throws Exception {
        if (jobTaskDefBean == null) {
            return -1;
        }
        try {
            jobTaskDefBean.setCreateTime(new Date());
            jobTaskDefBean.setUpdateTime(new Date());
            JobTaskDef insertBean = BeanUtil.toBean(JobTaskDef.class, BeanUtil.toMap(jobTaskDefBean));
            jobTaskDefMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteJobUserPermit(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobUserPermitMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobUserPermitBean> selectJobUserPermit(JobUserPermitBean condition) throws Exception {
        List<JobUserPermitBean> list = new ArrayList<JobUserPermitBean>();
        try {
            JobUserPermitExample example = new JobUserPermitExample();
            JobUserPermitExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getUserId() != null) {
                criteria.andUserIdEqualTo(condition.getUserId());
            }
            if (condition.getTaskId() != null) {
                criteria.andTaskIdEqualTo(condition.getTaskId());
            }


            List<JobUserPermit> queryDataItems = jobUserPermitMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobUserPermit jobUserPermit : queryDataItems) {
                list.add(BeanUtil.toBean(JobUserPermitBean.class, BeanUtil.toMap(jobUserPermit)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobUserPermitBean> selectJobUserPermitByCondition(JobUserPermitBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobUserPermit> page = PageUtil.startPage(pageParam);

        try {
            JobUserPermitExample example = new JobUserPermitExample();
            if (condition != null) {
                JobUserPermitExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<JobUserPermit> queryDataItems = jobUserPermitMapper.selectByExample(example);
            DefaultPageResult<JobUserPermitBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobUserPermitBean> list = new ArrayList<JobUserPermitBean>();
            for (JobUserPermit jobUserPermit : queryDataItems) {
                list.add(BeanUtil.toBean(JobUserPermitBean.class, BeanUtil.toMap(jobUserPermit)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobUserPermitByCondition(JobUserPermitBean condition) throws Exception {
        try {
            JobUserPermitExample example = new JobUserPermitExample();
            if (condition != null) {
                JobUserPermitExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return jobUserPermitMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobUserPermit(JobUserPermitBean jobUserPermitBean) throws Exception {
        if (jobUserPermitBean == null || jobUserPermitBean.getId() <= 0) {
            return false;
        }
        try {
            jobUserPermitBean.setUpdateTime(new Date());
            JobUserPermit updateBean = BeanUtil.toBean(JobUserPermit.class, BeanUtil.toMap(jobUserPermitBean));
            int updateNum = jobUserPermitMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobUserPermitBean selectJobUserPermitById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobUserPermit JobUserPermit = jobUserPermitMapper.selectByPrimaryKey(id);
            if (JobUserPermit == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobUserPermitBean.class, BeanUtil.toMap(JobUserPermit));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobUserPermit(JobUserPermitBean jobUserPermitBean) throws Exception {
        if (jobUserPermitBean == null) {
            return -1;
        }
        try {
            jobUserPermitBean.setCreateTime(new Date());
            jobUserPermitBean.setUpdateTime(new Date());
            JobUserPermit insertBean = BeanUtil.toBean(JobUserPermit.class, BeanUtil.toMap(jobUserPermitBean));
            jobUserPermitMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteJobUser(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = jobUserMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<JobUserBean> selectJobUser(JobUserBean condition) throws Exception {
        List<JobUserBean> list = new ArrayList<JobUserBean>();
        try {
            JobUserExample example = new JobUserExample();
            JobUserExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (!StringUtils.isEmpty(condition.getUserName())) {
                criteria.andUserNameEqualTo(condition.getUserName());
            }

            List<JobUser> queryDataItems = jobUserMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (JobUser JobUser : queryDataItems) {
                list.add(BeanUtil.toBean(JobUserBean.class, BeanUtil.toMap(JobUser)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<JobUserBean> selectJobUserByCondition(JobUserBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<JobUser> page = PageUtil.startPage(pageParam);

        try {
            JobUserExample example = new JobUserExample();
            if (condition != null) {
                JobUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }
                if (!StringUtils.isEmpty(condition.getUserName())) {
                    criteria.andUserNameEqualTo(condition.getUserName());
                }
            }

            List<JobUser> queryDataItems = jobUserMapper.selectByExample(example);
            DefaultPageResult<JobUserBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<JobUserBean> list = new ArrayList<JobUserBean>();
            for (JobUser JobUser : queryDataItems) {
                list.add(BeanUtil.toBean(JobUserBean.class, BeanUtil.toMap(JobUser)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countJobUserByCondition(JobUserBean condition) throws Exception {
        try {
            JobUserExample example = new JobUserExample();
            if (condition != null) {
                JobUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }
                if (!StringUtils.isEmpty(condition.getUserName())) {
                    criteria.andUserNameEqualTo(condition.getUserName());
                }

            }

            return jobUserMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateJobUser(JobUserBean jobUserBean) throws Exception {
        if (jobUserBean == null || jobUserBean.getId() <= 0) {
            return false;
        }
        try {
            jobUserBean.setUpdateTime(new Date());
            JobUser updateBean = BeanUtil.toBean(JobUser.class, BeanUtil.toMap(jobUserBean));
            int updateNum = jobUserMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public JobUserBean selectJobUserById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            JobUser JobUser = jobUserMapper.selectByPrimaryKey(id);
            if (JobUser == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(JobUserBean.class, BeanUtil.toMap(JobUser));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveJobUser(JobUserBean jobUserBean) throws Exception {
        if (jobUserBean == null) {
            return -1;
        }
        try {
            jobUserBean.setCreateTime(new Date());
            jobUserBean.setUpdateTime(new Date());
            JobUser insertBean = BeanUtil.toBean(JobUser.class, BeanUtil.toMap(jobUserBean));
            jobUserMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }
}
